// CheckPresenceOfFontsAndImages.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#define BUFFERSIZE 10000
char m_szInputBuffer[BUFFERSIZE];
char m_szFontName[BUFFERSIZE];

int CALLBACK EnumFontFamiliesExProc( ENUMLOGFONTEX *lpelfe, NEWTEXTMETRICEX *lpntme, int FontType, LPARAM lParam )
{
  // printf( "%s\n", lpelfe->elfFullName );
  return 0;  // returns 0 if there (also stops the looking)
}
void CheckFonts()
{
  printf(" XLingPaper-AnyFontsMissing-XLingPaper ");
  
  HDC hDC = GetDC( NULL );

  LOGFONT lf = { 0, 0, 0, 0, 0, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, "1" };
  int fFontIsNotThere = 0;
  bool bFoundMissingFont = false;
  bool bFoundKnownFontThatWillNotWork = false;
  char * cp = gets(m_szInputBuffer);
  while (cp != NULL && *cp != EOF && strcmp(cp, "XLingPaper-Images") != 0)
    {
      char *cp1 = strcpy(m_szFontName, cp);
      if (strcmp(_strlwr(cp1), "fixedsys") == 0)
	{  // check for known problematic font
	  bFoundKnownFontThatWillNotWork = true;
	  /* Note: if we find that there are more than one such font, then we
	     can use a data structure to store the font name and also 
	     information for a work-around.  We then store both of these in
	     the output and parse it in XXE to report to the user.
	  */
	}
      else
	{
	  strcpy(lf.lfFaceName, cp);
	  fFontIsNotThere = EnumFontFamiliesEx( hDC, &lf, (FONTENUMPROC)EnumFontFamiliesExProc, 0, 0 );
	}
      if (fFontIsNotThere == 1)
	{
	  printf("\"%s\" ", cp);
	  bFoundMissingFont = true;
	}
      cp = gets(m_szInputBuffer);
    }
  ReleaseDC( NULL, hDC );
  if (!bFoundMissingFont)
    printf("");
  printf(" XLingPaper-KnownFontsThatDoNotWork-XLingPaper ");
  if (bFoundKnownFontThatWillNotWork)
    {
      printf("\"Fixedsys\"");
    }
}

bool FileExists(char *cp)
{
  struct stat stFileInfo;
  bool bExists;
  int iStat;

  iStat = stat(cp, &stFileInfo);
  if (iStat == 0)
    {
      // We were able to get the file attributes so the file does exist
      bExists = true;
    }
  else
    {
      // We were not able to get the file attributes
      bExists = false;
    }
  return(bExists);
}

void CheckImages()
{
  printf(" XLingPaper-AnyImagesMissing-XLingPaper ");
  
  char * pszFileNameToCheck;
  char * pszURLFile = "file:///";
  bool bFoundMissingImage = false;
  char * cp = m_szInputBuffer;
  cp = gets(m_szInputBuffer);
  while (cp != NULL && *cp != EOF)
    {
#ifdef WIN32
	  // for Windows, need to change all forward slashes to backslashes
      char szAdjustWinFileName[BUFFERSIZE];
      char *pszWinFileName = strcpy(szAdjustWinFileName, cp);
      char *cp1 = strchr(pszWinFileName, '/');
      char *cp2;
	  int iSpacesCount = 0;
      while (cp1 != NULL)
	{
	  *cp1 = '\\';
	  cp1 = strchr(pszWinFileName, '/');
	}
      // also need to change the URI %20 to a space
      cp1 = cp2 = szAdjustWinFileName;
      while ((cp1 != NULL) && (*cp1!='\0'))
	{
	  if ((*cp1 == '%') && (*(cp1+1)=='2') && (*(cp1+2)=='0'))
	    {
	    *cp2++ = ' ';
	    cp1 += 3;
		iSpacesCount++;
	    }
	  else if ((*cp1 == '%') && (*(cp1+1)=='2') && (*(cp1+2)=='7'))
	    {
	    *cp2++ = '\'';
	    cp1 += 3;
		iSpacesCount++;
	    }
	  else
	    *cp2++ = *cp1++;
	}
	  szAdjustWinFileName[strlen(cp)-(2*iSpacesCount)] = '\0';
      pszFileNameToCheck = szAdjustWinFileName;
	  pszURLFile = "file:\\\\\\";
#else
      pszFileNameToCheck = cp;
#endif
	  if (strncmp(pszFileNameToCheck, pszURLFile, 8) == 0)
	  {
		  // skip the URL file indicator
		  pszFileNameToCheck = pszFileNameToCheck + 8;
	  }
      if (!FileExists(pszFileNameToCheck))
	{
	  // Convert any single quote/apostrophes to their HTML form
	  char szAdjustFileNameForQuotes[BUFFERSIZE];
	  //char *pszFileNameForQuotes = strcpy(szAdjustFileNameForQuotes, cp);
	  char *cp1;
	  cp1 = cp;
	  char *cp2 = szAdjustFileNameForQuotes;
	  while ((cp1 != NULL) && (*cp1!='\0'))
	    {
	      if (*cp1 == '\'')
		{
		  *cp2++ = '%';
		  *cp2++ = '2';
		  *cp2++ = '7';
		  cp1++;
		}
	      else 
		*cp2++ = *cp1++;
	    }
	  *cp2 = '\0';
	  printf("\"%s\" ", szAdjustFileNameForQuotes);
	  bFoundMissingImage = true;
	}
      cp = gets(m_szInputBuffer);
    }
  if (!bFoundMissingImage)
    printf("");
}
int main( int argc, char* argv[] )
{
  CheckFonts();
  CheckImages();
  return 0;
}

